//
//  SQHomeWorkViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/14.
//

#import "SQHomeWorkViewController.h"

#import "SQHomeWorkModel.h"

#import "SQWorkDetailsViewController.h"

#import "NSString+NCDate.h"

@interface SQHomeWorkViewController ()<BaseTableVCInterface>

@end

@implementation SQHomeWorkViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"作业检查";
    
    [self getHomeWorkData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getHomeWorkData];
    }];
    LC_WEAK_SELF
    self.cellEvent = ^(int key) {
        SQHomeWorkModelData * dataModel = weakSelf.dataSouece[key];
        SQWorkDetailsViewController * workDetailsVC = SQWorkDetailsViewController.new;
        workDetailsVC.detailsID = dataModel.sa_id;
        workDetailsVC.type = @"1";
        [weakSelf pushVC:workDetailsVC];
       
    };
    
}

-(void)getHomeWorkData{
    
    [SQHomeWorkModel getDataWithParams:@{@"m_id":UID,@"sa_enterprise":MEnterpriseId} WithCallBack:^(SQHomeWorkModel * data) {
        
        [self detailModelwithArr:data.data withCallBack:nil];
        [self reloadTableView];
        
    }];
}


-(NSInteger)rowsCount:(NSInteger)section{
    return self.dataSouece.count;
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.row];
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQHomeWorkTableViewCell"]];
}

- (void)tableViewSelectAtIndexPath:(NSIndexPath *)indexPath{
    SQHomeWorkModelData * dataModel = self.dataSouece[indexPath.row];
    if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 1  || [self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 0) { //未过期 和 正好
        
        if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_start substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == -1  || [self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_start substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == 0) {//已过期 和 正好
         if ([dataModel.sa_inspect isEqualToString:@"0"]) {
             SQWorkDetailsViewController * workDetailsVC = SQWorkDetailsViewController.new;
             workDetailsVC.detailsID = dataModel.sa_id;
             workDetailsVC.type = @"1";
             [self pushVC:workDetailsVC];
             
         }else if ([dataModel.sa_inspect isEqualToString:@"1"]){//完成
             SQWorkDetailsViewController * workDetailsVC = SQWorkDetailsViewController.new;
             workDetailsVC.detailsID = dataModel.sa_id;
             workDetailsVC.type = @"1";
             [self pushVC:workDetailsVC];
         }
         
         
     }
    
        
    }else if ([self compareOneDay:[NSString dateFromTimeStr:[NSString formateDateToDay:[dataModel.sa_end substringWithRange:NSMakeRange(6,13)]]] withAnotherDay:[NSString getCurrentTime]] == -1){
        if ([dataModel.sa_inspect isEqualToString:@"1"]){//完成
            SQWorkDetailsViewController * workDetailsVC = SQWorkDetailsViewController.new;
            workDetailsVC.detailsID = dataModel.sa_id;
            workDetailsVC.type = @"1";
            [self pushVC:workDetailsVC];
        }
      
    }
}



- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
NSComparisonResult result = [dateA compare:dateB];
NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
if (result == NSOrderedDescending) {
    //在指定时间前面 过了指定时间 过期
    NSLog(@"oneDay  is in the future");
    return 1;
}
else if (result == NSOrderedAscending){
    //没过指定时间 没过期
    //NSLog(@"Date1 is in the past");
    return -1;
}
//刚好时间一样.
//NSLog(@"Both dates are the same");
return 0;

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
